package com.log.service.Impl;

import com.log.common.util.PageHelper;
import com.log.common.vo.ResStatus;
import com.log.common.vo.ResultVo;
import com.log.dao.LogDepotMapper;
import com.log.dao.LogGoodsMapper;
import com.log.dao.LogGoodsOutInDetailMapper;
import com.log.dao.LogGoodsOutInMapper;
import com.log.pojo.LogDepot;
import com.log.pojo.LogGoods;
import com.log.pojo.LogGoodsOutIn;
import com.log.pojo.LogGoodsOutInDetail;
import com.log.pojoVo.LogGoodsListVo;
import com.log.pojoVo.LogGoodsVo;
import com.log.service.LogGoodsInService;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author wjy
 * @create 2023-10-2023/10/16 14:54
 */
@Service
public class LogGoodsInServiceImpl implements LogGoodsInService {
    @Autowired
    private LogGoodsOutInMapper logGoodsOutInMapper;
    @Autowired
    private LogGoodsOutInDetailMapper logGoodsOutInDetailMapper;
    @Autowired
    private LogDepotMapper logDepotMapper;
    @Autowired
    private LogGoodsMapper logGoodsMapper;
    @Override
    @Transactional
    public ResultVo insertLogGoodsOutIn(LogGoodsListVo list) {
        Date now = new Date();
        List<LogGoodsVo> logGoodsVoList= list.getList();
        //1.预插入，获得主键id
        LogGoodsOutIn logGoodsOutIn = new LogGoodsOutIn();
        logGoodsOutIn.setStatus(0);
        logGoodsOutInMapper.insertUseGeneratedKeys(logGoodsOutIn);
        double log_goods_out_total_price=0;//计算总价
        int log_goods_out_num=0;//计算总数
        StringBuilder builder = new StringBuilder();

        //2.1判断对应商品库存是否足够
        for (int i = 0; i < logGoodsVoList.size(); i++) {
            //1.当前拿出商品
            LogGoodsVo logGoods = logGoodsVoList.get(i);
            //2.拿出当前商品出库数量,并且计入总数量
            int logGoodsNum = logGoodsVoList.get(i).getLogGoodsNum();
            if (logGoods.getLogGoodsStoke() < logGoodsNum) {
                //库存不够
                return new ResultVo(ResStatus.NO, "商品库存不足够！", null);
            }
        }
        //所有商品库存都足够
        for (int i = 0; i < logGoodsVoList.size(); i++) {
            //1.当前拿出商品
            LogGoodsVo logGoods = logGoodsVoList.get(i);
            //2.拿出当前商品出库数量,并且计入总数量
            int logGoodsNum = logGoodsVoList.get(i).getLogGoodsNum();
            log_goods_out_num+=logGoodsNum;
            //扣减库存
            LogGoods logGoodsNew = new LogGoods();
            //更新每个商品的出库时间
            logGoods.setLogGoodsOutTime(new Date());
            logGoodsNew.setLogGoodsId(logGoods.getLogGoodsId());
            logGoodsNew.setLogGoodsStoke(logGoods.getLogGoodsStoke()-logGoodsNum);
            logGoodsMapper.updateByPrimaryKeySelective(logGoodsNew);
            //.写出库商品详细表
            LogGoodsOutInDetail logGoodsOutInDetail = new LogGoodsOutInDetail();
            logGoodsOutInDetail.setLogGoodsOutInId(logGoodsOutIn.getLogGoodsOutId());
            logGoodsOutInDetail.setLogGoodsId(logGoods.getLogGoodsId());
            logGoodsOutInDetail.setLogGoodsName(logGoods.getLogGoodsName());
            logGoodsOutInDetail.setLogGoodsPrice(logGoods.getLogGoodsPrice());
            logGoodsOutInDetail.setLogGoodsVolume(logGoods.getLogGoodsVolume());
            logGoodsOutInDetail.setLogGoodsNum(logGoodsNum);
            logGoodsOutInDetail.setLogGoodsTotalVolume(logGoods.getLogGoodsVolume()*logGoodsNum);
            double v = logGoods.getLogGoodsPrice() * logGoodsNum;
            log_goods_out_total_price+=v;
            logGoodsOutInDetail.setLogGoodsTotalValue(v);
            logGoodsOutInDetail.setLogCreatedTime(now);
            //拼接名称
            if(i==logGoodsVoList.size()-1)
            {
                builder.append(logGoods.getLogGoodsName());
            }else{
                builder.append(logGoods.getLogGoodsName());
                builder.append(",");
            }
            logGoodsOutInDetailMapper.insert(logGoodsOutInDetail);
        }
        logGoodsOutIn.setLogGoodsNames(builder.toString());
        logGoodsOutIn.setLogGoodsOutNum(log_goods_out_num);
        logGoodsOutIn.setLogGoodsOutTotalPrice(log_goods_out_total_price);
        logGoodsOutIn.setLogGoodsOutTime(now);
        logGoodsOutIn.setLogGoodsOutDepot(list.getCurrentLogDepotName());
        logGoodsOutIn.setLogGoodsOutToDepot(list.getOutToLogDepotName());
        logGoodsOutIn.setLogGoodsOutDes(list.getLogGoodsOutDes());
        logGoodsOutIn.setLogGoodsOutOperator(list.getLogGoodsOutOperator());
        logGoodsOutInMapper.updateByPrimaryKeySelective(logGoodsOutIn);
        return new ResultVo(ResStatus.OK,"添加出库单成功!",null);
    }

    @Override
    public ResultVo getLogGoodsInByPage(int pageNum, int limit) {
        //1.起始索引
        int start = (pageNum-1)*limit;
        //2.查询结果
        List<LogGoodsOutIn> list = logGoodsOutInMapper.selectLogGoodsInByPage(start, limit);
        //3.查询总数
        Example example = new Example(LogGoodsOutIn.class);
        Example.Criteria criteria = example.createCriteria();
        int count = logGoodsOutInMapper.selectCountByExample(example);
        //3.计算总页数
        int pageCount = count%limit==0 ? count/limit:count/limit+1;
        return new ResultVo(ResStatus.OK,"success",new PageHelper<LogGoodsOutIn>(count,pageCount,list));
    }

    @Override
    public ResultVo getLogGoodsInById(int id) {
        LogGoodsOutIn logGoodsOutIn = logGoodsOutInMapper.selectByPrimaryKey(id);
        return logGoodsOutIn==null?new ResultVo(ResStatus.NO,"记录不存在",logGoodsOutIn):new ResultVo(ResStatus.OK,"success",logGoodsOutIn);
    }

    @Override
    @Transactional
    public ResultVo confirmLogGoodsIn(int id) {
        //1.修改入库状态
        LogGoodsOutIn logGoodsOutIn = new LogGoodsOutIn();
        logGoodsOutIn.setLogGoodsOutId(id);
        logGoodsOutIn.setStatus(4);//设置4，就是确定入库
        //2.通过id查询商品入库明细
        Example example = new Example(LogGoodsOutInDetail.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("logGoodsOutInId",id);
        List<LogGoodsOutInDetail> list = logGoodsOutInDetailMapper.selectByExample(example);
        double total= list.stream().mapToDouble(LogGoodsOutInDetail::getLogGoodsTotalVolume).sum();
        //2.1更新出库时间
        list.stream().forEach(s->{
            LogGoods logGoods = new LogGoods();
            logGoods.setLogGoodsId(s.getLogGoodsId());
            logGoods.setLogGoodsInTime(new Date());
            logGoodsMapper.updateByPrimaryKeySelective(logGoods);
        });
        //3.修改仓库容量
        LogGoodsOutIn logGoodsOutIn1 = logGoodsOutInMapper.selectByPrimaryKey(id);
        Example example1 = new Example(LogDepot.class);
        Example.Criteria criteria1 = example1.createCriteria();
        criteria1.andEqualTo("logDepotName",logGoodsOutIn1.getLogGoodsOutToDepot());
        List<LogDepot> logDepots = logDepotMapper.selectByExample(example1);
        LogDepot logDepot = logDepots.get(0);
        if(total<=logDepot.getLogDepotRemainder())
        {
            //容量可入库
            logDepot.setLogDepotRemainder(logDepot.getLogDepotRemainder()-total);
            int i = logGoodsOutInMapper.updateByPrimaryKeySelective(logGoodsOutIn);
            logDepotMapper.updateByPrimaryKeySelective(logDepot);
            return new ResultVo(ResStatus.OK,"入库成功",null);
        }else{
            return new ResultVo(ResStatus.NO,"入库失败,仓库容量不足",null);
        }
    }

    @Override
    public ResultVo getLogGoodsInDetails(int id) {
        //通过id查询商品入库明细
        Example example = new Example(LogGoodsOutInDetail.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("logGoodsOutInId",id);
        List<LogGoodsOutInDetail> list = logGoodsOutInDetailMapper.selectByExample(example);
        return list.size()>0?new ResultVo(ResStatus.OK,"查询成功",list):new ResultVo(ResStatus.NO,"查询失败，没有数据！",null);
    }

    @Override
    public ResultVo getCurrentLogGoodsInByPage(int pageNum, int limit, String CurrentDepot) {
        //1.起始索引
        int start = (pageNum-1)*limit;
        //2.查询结果
        List<LogGoodsOutIn> list=null;
        Example example = new Example(LogGoodsOutIn.class);
        Example.Criteria criteria = example.createCriteria();
        if(CurrentDepot==null||CurrentDepot.equals(""))
        {
            list = logGoodsOutInMapper.selectLogGoodsInByPage(start, limit);
        }else{
            list = logGoodsOutInMapper.selectCurrentLogGoodsInByPage(start, limit,CurrentDepot);
            //3.查询总数
            criteria.andEqualTo("logGoodsOutToDepot",CurrentDepot);
        }
        int count = logGoodsOutInMapper.selectCountByExample(example);
        //3.计算总页数
        int pageCount = count%limit==0 ? count/limit:count/limit+1;
        return new ResultVo(ResStatus.OK,"success",new PageHelper<LogGoodsOutIn>(count,pageCount,list));
    }

    @Override
    public ResultVo getCurrentNoLogGoodsInByPage(int pageNum, int limit, String CurrentDepot,int status) {
        //1.起始索引
        int start = (pageNum-1)*limit;
        //2.查询结果
        List<LogGoodsOutIn> list=null;
        //3.查询总数
        Example example = new Example(LogGoodsOutIn.class);
        Example.Criteria criteria = example.createCriteria();
        if(CurrentDepot==null||CurrentDepot.equals(""))
        {
            list = logGoodsOutInMapper.selectLogGoodsInByPage(start, limit);
        }else{
            list = logGoodsOutInMapper.selectCurrentNoLogGoodsInByPage(start, limit,CurrentDepot,status);
            criteria.andEqualTo("logGoodsOutToDepot",CurrentDepot);
            criteria.andEqualTo("status",status);
        }
        int count = logGoodsOutInMapper.selectCountByExample(example);
        //3.计算总页数
        int pageCount = count%limit==0 ? count/limit:count/limit+1;
        return new ResultVo(ResStatus.OK,"success",new PageHelper<LogGoodsOutIn>(count,pageCount,list));
    }


}
